//
//  ListViewController.m
//  Xing
//
//  Created by 徐番茄丶 on 16/1/21.
//  Copyright © 2016年 徐番茄丶. All rights reserved.
//

#import "ListViewController.h"
#import "MyTools.h"
#import "ListTableViewCell.h"
@interface ListViewController ()<UITableViewDataSource,UITableViewDelegate>{
    UITableView *_myTab;
    NSArray     *_myarr;
}

@end

@implementation ListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"提现记录";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    [self.navigationItem.leftBarButtonItem setTintColor:UIColorFromRGB(0xffffff)];
    
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    
    self.view.backgroundColor = UIColorFromRGB(0xefeff1);
    
    
    
}

-(void)getDate{

    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic1 = @{@"user_token":[user objectForKey:@"token"],
                           @"page":@"1"};
    
    AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [manager POST:CASHLOG_URL parameters:dic1 success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
            _myarr = [[NSArray alloc]initWithArray:[responseObject objectForKey:@"data"]];
            
            [self viewCreate];
            //[_myTab reloadData];
        }else{
            [self.view makeToast:[responseObject objectForKey:@"info"] duration:1.0 position:CSToastPositionCenter];
        }
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
    }];
}


-(void)viewCreate{
    _myTab = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
    _myTab.separatorStyle = UITableViewCellSeparatorStyleNone;
    _myTab.showsVerticalScrollIndicator = NO;
    
//    [_myTab registerNib:[UINib nibWithNibName:@"ListTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    _myTab.dataSource = self;
    _myTab.delegate = self;
    [self.view addSubview:_myTab];
}



#pragma mark tableview代理

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _myarr.count + 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *Identifier = @"cell";
    ListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
    if (cell == nil) {
        cell = [[ListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    cell.userInteractionEnabled = NO;
    if (indexPath.row == 0) {
        cell.timelab.textColor = UIColorFromRGB(0x333333);
        cell.moneylab.textColor = UIColorFromRGB(0x333333);
        cell.numlab.textColor = UIColorFromRGB(0x333333);
        cell.statelab.textColor = UIColorFromRGB(0x333333);
        cell.typelab.backgroundColor = [UIColor clearColor];
        
        cell.timelab.font = [UIFont systemFontOfSize:15];
        cell.moneylab.font = [UIFont systemFontOfSize:15];
        cell.numlab.font = [UIFont systemFontOfSize:15];
        cell.statelab.font = [UIFont systemFontOfSize:15];
        
        cell.timelab.text = @"日期";
        cell.moneylab.text = @"金额";
        cell.numlab.text = @"提现人";
        cell.statelab.text = @"状态";
        cell.backgroundColor = UIColorFromRGB(0xefeff1);
    }else{
        
        NSDictionary *dic = [[NSDictionary alloc]initWithDictionary:[_myarr objectAtIndex:indexPath.row - 1]];
        
        cell.timelab.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"create_time"]];
        cell.moneylab.text = [NSString stringWithFormat:@"￥%@",[dic objectForKey:@"price"]];
        cell.statelab.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"status"]];
        if ([[dic objectForKey:@"pay_way"] isEqualToString:@"alipay_wap"]) {
            cell.numlab.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"truename"]];
            cell.typelab.text = @"支付宝";
            cell.typelab.backgroundColor = UIColorFromRGB(0x03A9F4);
        }else if ([[dic objectForKey:@"pay_way"] isEqualToString:@"wx_pub"]){
            cell.numlab.text = [NSString stringWithFormat:@"%@",[dic objectForKey:@"truename"]];
            cell.typelab.text =@"微信";
            cell.typelab.backgroundColor = UIColorFromRGB(0x8BC34A);
        }
        //cell.typelab.layer.cornerRadius = 10;
    }
    
    return cell;
}
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//}






-(void)back{
    if ([_push isEqualToString:@"1"]) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.translucent = YES;
    self.tabBarController.tabBar.hidden = YES;
    
    [self getDate];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
